<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
<head>
<!-- jQuery 2.1.4 -->
<script src="<c:url value="/resources/plugins/jQuery/jQuery-2.1.4.min.js" />"></script>
	<title>Java API for WebSocket (JSR-356)</title>  
</head>
<body>
<script src="https://cdn.bootcss.com/sockjs-client/1.0.3/sockjs.min.js"></script>
<script type="text/javascript">
	var websocket = null;
	var url = "ws://localhost:8080//OA/webSocketServer;jsessionid=<%=request.getSession(true).getId()  %>";
	if ('WebSocket' in window) {
		
		websocket = new WebSocket(url);
	} 
	else if ('MozWebSocket' in window) {
		websocket = new MozWebSocket(url);
	} 
	else {
		websocket = new SockJS("http://localhost:8080//OA/sockjs/webSocketServer");
	}
	websocket.onopen = onOpen;
	websocket.onmessage = onMessage;
	websocket.onerror = onError;
	websocket.onclose = onClose;
	      	
	function onOpen(openEvt) {
		$("#content").append("连接成功");
	}
	
	function onMessage(evt) {
		$("#content").append("<br>"+evt.data);
		console.log(evt);
	}
	function onError() {}
	function onClose() {}
	
	function doSend() {
		if (websocket.readyState == websocket.OPEN) {  		
            var msg = document.getElementById("inputMsg").value;  
            websocket.send(msg);//调用后台handleTextMessage方法
           
        } else {  
        	alert("连接失败!");  
        }  
	}
</script>
请输入：<textarea rows="5" cols="10" id="inputMsg" name="inputMsg"></textarea>
<button onclick="doSend();">发送</button>
<div id="content" ></div>
</body>
</html>
